package com.zcc.makingfriend.service.impl;

import com.zcc.makingfriend.dao.User;
import com.zcc.makingfriend.dao.WxLogin;
import com.zcc.makingfriend.dto.PageDto;
import com.zcc.makingfriend.mapper.UserMapper;
import com.zcc.makingfriend.mapper.WxLoginMapper;
import com.zcc.makingfriend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

/**
 * @author xiaomi
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WxLoginMapper wxLoginMapper;

    @Override
    public List<User> getAll() {
        List<User> users = userMapper.getAll();
        return users;
    }

    @Override
    public User getUserByName(String name) {
        User userByName = userMapper.getUserByName(name);
        return userByName;
    }

    @Override
    public int addUser(User user) {
        int i = userMapper.addUser(user);
        return i;
    }

    @Override
    public int updateUser(User user) {
        int i = userMapper.updateUser(user);
        boolean checkTel = checkTel(user.getTel());
        boolean checkEmail = checkEmail(user.getEmail());
        if (checkTel) {
            if (checkEmail) {
                return i;
            } else {
                return -2;
            }
        } else {
            return -1;
        }
    }

    @Override
    public User findUserByUuid(String uuid) {
        User user = userMapper.findUserByUuid(uuid);
        return user;
    }

    @Override
    public User findUserById(Long id) {
        User user = userMapper.findUserById(id);
        return user;
    }

    @Override
    public int deleteUser(String uuid) {
        int i = userMapper.deleteUser(uuid);
        if (i == 1) {
            int j = wxLoginMapper.deleteWxLogin(uuid);
            return j;
        }
        return i;
    }

    @Override
    public Long countUsers() {
        return userMapper.countUsers();
    }

    @Override
    public List<User> findUsersByCondition(PageDto pageDto) {
        pageDto.setPage((pageDto.getPage() - 1) * pageDto.getSize());
        return userMapper.findUsersByCondition(pageDto);
    }

    @Override
    public Long countUsersByCondition(PageDto pageDto) {
        return userMapper.countUsersByCondition(pageDto);
    }


    public static boolean checkTel(String tel) {
        if (tel != null && (!tel.isEmpty())) {
            return Pattern
                    .matches("^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$", tel);
        }
        return true;
    }

    public static boolean checkEmail(String email) {
        if (email != null && (!email.isEmpty())) {
            return Pattern
                    .matches("^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$", email);
        }
        return true;
    }
}
